package org.example.controller;

import cn.hutool.json.JSONObject;
import org.example.model.Member;
import org.example.model.User;
import org.example.service.MemberService;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private UserService userService;

    //新增抽奖人员
    @PostMapping("/add")
    public Object add(@RequestBody Member member, HttpSession session) {
//        User user = (User) session.getAttribute("user");
//        if(user != null) {
//
//        }
        member.setSettingId(1);
        int n = memberService.add(member);
        return member.getId();
    }

    //修改抽奖人员
    @PostMapping("/update")
    public Object update(@RequestBody Member member) {
        int n = memberService.update(member);
        return null;
    }

    //删除抽奖人员
    @GetMapping("/delete/{id}")
    public Object delete(@PathVariable Integer id, HttpServletRequest req) {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        User exist = userService.query(user.getUsername());
        if (exist == null || !user.getUsername().equals("root")) {
            JSONObject json = new JSONObject();
            json.append("msg", "你没有删除的权限");
            return json;
        }

        int n = memberService.delete(id);
        return null;
    }
}
